package algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Calendar;
import bean.Credit_parameter;
public class Login_credit {
	
	
	//参数（连续登录天数，上一次登录时间）
	public Map<String, Integer> Get_logindetails(int logindays,Date logindate){    //获取连续登录天数
		int credit = 0;
		Calendar nowcal = Calendar.getInstance();
		Calendar pastcal = Calendar.getInstance();
		pastcal.setTime(logindate);   //return值，表示获得的积分数
		
		int nowday = nowcal.get(Calendar.DAY_OF_YEAR);
		int nowyear = nowcal.get(Calendar.YEAR);
		int nowmonth = nowcal.get(Calendar.MONTH);
		long nowtime = nowcal.getTimeInMillis();
		int pastday = pastcal.get(Calendar.DAY_OF_YEAR);
		int pastyear = pastcal.get(Calendar.YEAR);
		int pastmonth = pastcal.get(Calendar.MONTH);
		long pasttime = pastcal.getTimeInMillis();
		boolean flag = true;
		//判断是否连续登陆
		if(nowtime == pasttime){  //是否第一次登录
			logindays++;
			flag = true;
		}
		else if(nowyear == pastyear && nowday == pastday){   // 是否一天内多次登陆
			flag = false;
		}
		else if(nowyear == pastyear && nowday - pastday == 1){ //是否连续登陆
			logindays++;
			flag = true;
		}
		else if(nowyear - pastyear == 1 && pastmonth == 12 && pastday == 31 && nowmonth == 1 && nowday == 1){
			logindays++;
			flag = true;
		}
		else{                                                //非连续登录
			logindays = 1;
			flag = true;
		}
		
		//计算登录加分奖励,连续登陆一周获得18个积分点
		if(flag){
			if(logindays % 7 == 0){   
				credit = Credit_parameter.getLogon_7();   
			}
			else if(logindays % 7 == 6){
				credit = Credit_parameter.getLogon_6();
			}
			else if(logindays % 7 == 5){
				credit = Credit_parameter.getLogon_5();
			}
			else if(logindays % 7 == 4){
				credit = Credit_parameter.getLogon_4();
			}
			else if(logindays % 7 == 3){
				credit = Credit_parameter.getLogon_3();
			}
			else if(logindays % 7 == 2){
				credit = Credit_parameter.getLogon_2();
			}
			else if(logindays % 7 == 1){
				credit = Credit_parameter.getLogon_1();
			}
		}
		
		Map<String, Integer> map =new HashMap<String, Integer>();   //返回map对象，map<连续登录天数，获得积分值>
		map.put("logindays", logindays);
		map.put("credit", credit); 
		
		//积分变化
		if(credit == 0){
			map.put("flag", 0);     //积分没变化
		}
		else{
			map.put("flag", 1);      //积分变化
		}
		return map;
	}
}
